package com.fr.base;

import com.fr.base.dav.Env;
import com.fr.base.file.CacheManager;
import com.fr.base.file.ClusterConfigManager;
import com.fr.base.file.ConfigManager;
import com.fr.base.file.DatasourceManager;
import com.fr.base.file.FaceManager;
import com.fr.base.file.FunctionManager;
import com.fr.data.impl.Connection;
import com.fr.data.impl.JDBCDatabaseConnection;
import com.fr.privilege.manager.PrivilegeManager;
import com.fr.report.io.xml.SynchronizedVersion;
import com.fr.report.web.ui.WidgetManager;
import com.fr.web.platform.PlatformManager;
import com.fr.web.platform.module.ModuleManager;
import java.io.InputStream;
import java.util.Locale;
import java.util.logging.ConsoleHandler;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;

public class FRContext
{
  private static FunctionManager functionManager = null;
  private static PlatformManager platformManager = null;
  private static PrivilegeManager privilegeManager = null;
  private static FaceManager faceManager = null;
  private static ConfigManager configManager = null;
  private static ModuleManager moduleManager = null;
  private static DatasourceManager datasourceManager = null;
  private static CacheManager cacheManager = null;
  private static WidgetManager widgetManager = null;
  private static ClusterConfigManager clusterConfigManager = null;
  private static Locale locale = Locale.CHINA;
  private static Logger logger = null;
  private static DefaultValues defaultValues = null;
  private static Env env = null;

  public static synchronized JDBCDatabaseConnection getPlatformConnection()
  {
    Connection localConnection = getDatasourceManager().getConnection("fr_platform");
    if (localConnection instanceof JDBCDatabaseConnection)
      return ((JDBCDatabaseConnection)localConnection);
    return null;
  }

  public static synchronized void setPlatformConnection(JDBCDatabaseConnection paramJDBCDatabaseConnection)
  {
    getDatasourceManager().putConnection("fr_platform", paramJDBCDatabaseConnection);
  }

  public static synchronized FunctionManager getFunctionManager()
  {
    if (functionManager == null)
    {
      functionManager = new FunctionManager();
      functionManager.readXMLFile();
    }
    return functionManager;
  }

  public static synchronized void setFunctionManager(FunctionManager paramFunctionManager)
  {
    functionManager = paramFunctionManager;
  }

  public static synchronized PlatformManager getPlatformManager()
  {
    if (platformManager == null)
    {
      platformManager = new PlatformManager();
      platformManager.readXMLFile();
    }
    return platformManager;
  }

  public static synchronized PrivilegeManager getPrivilegeManager()
  {
    if (privilegeManager == null)
    {
      privilegeManager = new PrivilegeManager();
      privilegeManager.readXMLFile();
    }
    return privilegeManager;
  }

  public static synchronized void setPrivilegeManager(PrivilegeManager paramPrivilegeManager)
  {
    privilegeManager = paramPrivilegeManager;
  }

  public static synchronized ModuleManager getModuleManager()
  {
    if (moduleManager == null)
    {
      moduleManager = new ModuleManager();
      moduleManager.readXMLFile();
    }
    return moduleManager;
  }

  public static synchronized void setModuleManager(ModuleManager paramModuleManager)
  {
    moduleManager = paramModuleManager;
  }

  public static synchronized FaceManager getFaceManager()
  {
    if (faceManager == null)
    {
      faceManager = new FaceManager();
      faceManager.readXMLFile();
    }
    return faceManager;
  }

  public static synchronized void setFaceManager(FaceManager paramFaceManager)
  {
    faceManager = paramFaceManager;
  }

  public static synchronized DatasourceManager getDatasourceManager()
  {
    if (datasourceManager == null)
    {
      datasourceManager = new DatasourceManager();
      datasourceManager.readXMLFile();
    }
    SynchronizedVersion.removeSynchronizedVersion(Thread.currentThread());
    return datasourceManager;
  }

  public static synchronized void setDatasourceManager(DatasourceManager paramDatasourceManager)
  {
    datasourceManager = paramDatasourceManager;
  }

  public static synchronized void refreshDatasourceManager()
  {
    DatasourceManager localDatasourceManager = new DatasourceManager();
    if (!(localDatasourceManager.readXMLFile()))
      return;
    SynchronizedVersion.removeSynchronizedVersion(Thread.currentThread());
    datasourceManager = localDatasourceManager;
  }

  public static synchronized WidgetManager getWidgetManager()
  {
    if (widgetManager == null)
    {
      widgetManager = new WidgetManager();
      widgetManager.readXMLFile();
    }
    return widgetManager;
  }

  public static synchronized void setWidgetManager(WidgetManager paramWidgetManager)
  {
    widgetManager = paramWidgetManager;
  }

  public static synchronized ClusterConfigManager getClusterConfigManager()
  {
    if (clusterConfigManager == null)
    {
      clusterConfigManager = new ClusterConfigManager();
      clusterConfigManager.readXMLFile();
    }
    return clusterConfigManager;
  }

  public static synchronized void setClusterConfigManager(ClusterConfigManager paramClusterConfigManager)
  {
    clusterConfigManager = paramClusterConfigManager;
  }

  public static synchronized CacheManager getCacheManager()
  {
    if (cacheManager == null)
    {
      cacheManager = new CacheManager();
      cacheManager.readXMLFile();
    }
    return cacheManager;
  }

  public static synchronized void setCacheManager(CacheManager paramCacheManager)
  {
    cacheManager = paramCacheManager;
  }

  public static synchronized ConfigManager getConfigManager()
  {
    if (configManager == null)
    {
      configManager = new ConfigManager();
      if (!(configManager.readXMLFile()))
      {
        InputStream localInputStream = getResourceStream("ReportServerParameter.rxml");
        if (localInputStream != null)
          try
          {
            XMLTools.readInputStreamXML(configManager, localInputStream);
          }
          catch (Exception localException)
          {
            getLogger().log(Level.WARNING, localException.getMessage(), localException);
          }
      }
    }
    return configManager;
  }

  public static synchronized void setConfigManager(ConfigManager paramConfigManager)
  {
    configManager = paramConfigManager;
  }

  public static Locale getLocale()
  {
    return locale;
  }

  public static void setLocale(Locale paramLocale)
  {
    if (paramLocale == null)
      return;
    locale = paramLocale;
  }

  public static void setLanguage(int paramInt)
  {
    switch (paramInt)
    {
    case 2:
      locale = Locale.US;
      break;
    case 3:
      locale = Locale.JAPAN;
      break;
    case 1:
      locale = Locale.CHINA;
      break;
    case 4:
      locale = Locale.TAIWAN;
      break;
    default:
      locale = Locale.CHINA;
    }
  }

  public static Logger getLogger()
  {
    if (logger == null)
    {
      logger = Logger.getLogger("FR");
      logger.setLevel(Level.WARNING);
      logger.setUseParentHandlers(false);
      logger.addHandler(new ConsoleHandler()
      {
        public void publish(LogRecord paramLogRecord)
        {
          paramLogRecord.setSourceClassName("");
          super.publish(paramLogRecord);
        }
      });
    }
    return logger;
  }

  public static DefaultValues getDefaultValues()
  {
    if (defaultValues == null)
      defaultValues = new DefaultValues();
    return defaultValues;
  }

  public static void setDefaultValues(DefaultValues paramDefaultValues)
  {
    defaultValues = paramDefaultValues;
  }

  public static InputStream getResourceStream(String paramString)
  {
    Env localEnv = getCurrentEnv();
    if (localEnv != null)
      try
      {
        InputStream localInputStream;
        if ((localInputStream = localEnv.readResource(paramString)) != null)
          getLogger().log(Level.INFO, Inter.getLocText("Load_Resource_File") + ":\"" + paramString + "\".");
        return localInputStream;
      }
      catch (Exception localException)
      {
      }
    return null;
  }

  public static Env getCurrentEnv()
  {
    return env;
  }

  public static void setCurrentEnv(Env paramEnv)
  {
    env = paramEnv;
    configManager = null;
    datasourceManager = null;
    widgetManager = null;
    faceManager = null;
    functionManager = null;
    privilegeManager = null;
    moduleManager = null;
    cacheManager = null;
    clusterConfigManager = null;
    defaultValues = null;
  }
}